package aad.storyboard.model;

import java.util.ArrayList;

public class Chapter extends StoryComponent{

    
    private ArrayList <Scene> scenes=new ArrayList<Scene> ();
    private Scene currentScene;     
    private boolean isStarted=false;
    
   
      
      
    public interface ChapterChangeListener {       
        void onChapterChange(Chapter chapter);
    }

    public Chapter(int id,int startTime){       
        super (id,startTime); 
    
    }
   
    public Chapter(int id) {
		super(id);
	}

	
    
    public Scene getNextScene(Scene scene)
    {
    	int i=1;
    	for (Scene sc: getScenes())
    	{
    		if (sc.equals(scene)&&i<scenes.size())
    		{
    			return (getScenes().get(i));
    		}
    		i++;
    	}
    	return null;
    }
    
    public Boolean chapterStartTimeAchieved(int remTime)
    {
    if (startTime==remTime)
    {
    	//Start showing scenes
    	setCurrentScene(getScene(remTime));
    	return true;
    }
    return false;
    }

    public Scene getScene(int startTime)
    {
     for (Scene s: getScenes())
     {
    	 if (s.getStartTime()==startTime)
    	 {
    		 return s;
    	 }
     }
     return null;
    }    
	
    
	public void addScene(Scene c)	{
		int index = 0;
		for(Scene s : this.scenes){
			if(s.getId() > c.getId())
				break;
			index++;
		}
		this.getScenes().add(index,c);	
	}
	
	public void removeScene(Scene c)
	{
		this.getScenes().remove(c);
	}
	
	
	public void setCurrentScene(Scene scene) {
		this.currentScene = scene;
	}

	public Scene getCurrentScene() {
		return currentScene;
	}

		public ArrayList <Scene> getScenes() {
		return scenes;
	}

		public void setCurrentScene(int i) {
			this.currentScene=scenes.get(i);
			
		}

		public Scene getPreviousScene(Scene currentScene) {
			int i=this.scenes.size();
			for (Scene sc: getScenes())
	    	{
	    		if (sc.equals(currentScene)&&i>=1)
	    		{
	    			return (getScenes().get(i-1));
	    		}
	    		i--;
	    	}
			return null;
		}

		public boolean isStarted() {
			
			return isStarted;
		}
		
		public void setStarted(boolean start) {
			
			isStarted=start;
		}

		
		public Scene getNextScene() {
			Scene scene=null;
			for (int i=0; i<scenes.size();i++)
			{
				if (scenes.get(i).equals(this.getCurrentScene())&&scenes.get(i+1)!=null)
				{
					
				scene=scenes.get(i+1);
				
				return scene;
				}
			}
			return scene;
		}

		public Scene getPreviousScene() {
			Scene scene=null;
			for (int i=scenes.size()-1; i>1;i--)
			{
				if (scenes.get(i).equals(this.getCurrentScene()))
				{
					
				scene=scenes.get(i-1);
				
				return scene;
				}
			}
			return scene;
		}
}
